//
//  MyEmblemViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/29.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "MyEmblemViewController.h"
#import "MyEmblemCell.h"
#import "MyEmblemTwoCell.h"
#import "MyEmblemThreeCell.h"
#import "MyEmblemFourCell.h"
@interface MyEmblemViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *firstImagesArray;
@property(nonatomic,strong)NSArray *firstTitlesArray;
@property(nonatomic,strong)NSArray *twoImagesArray;
@property(nonatomic,strong)NSArray *twoTitlesArray;
@property(nonatomic,strong)NSArray *threetImagesArray;
@property(nonatomic,strong)NSArray *threeTitlesArray;
@property(nonatomic,strong)NSArray *fourImagesArray;
@property(nonatomic,strong)NSArray *fourTitlesArray;
@end

@implementation MyEmblemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"途吉徽章";
    [self createNavagationBackBtn];
    [self createTabelveiew];
    // Do any additional setup after loading the view.
}
-(void)createNavagationBackBtn{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 6, 67, 23);
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
    backImg.frame = CGRectMake(-10, 0, 22, 22);
    [backBtn addSubview:backImg];
    UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 65, 22)];
//    backText.text = @"返回";
    backText.font = [UIFont systemFontOfSize:17];
    [backText setBackgroundColor:[UIColor clearColor]];
    [backText setTextColor:[UIColor whiteColor]];
    [backBtn addSubview:backText];
    [backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    [backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backBtn.mas_left).offset(-10);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [backText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backImg.mas_right).offset(2);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(65, 22));
    }];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];
}
-(void)leftBarButtonItemPressed:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)createTabelveiew{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[MyEmblemCell class] forCellReuseIdentifier:@"MyEmblemCell"];
        [self.tableView registerClass:[MyEmblemTwoCell class] forCellReuseIdentifier:@"MyEmblemTwoCell"];
        [self.tableView registerClass:[MyEmblemThreeCell class] forCellReuseIdentifier:@"MyEmblemThreeCell"];
        [self.tableView registerClass:[MyEmblemFourCell class] forCellReuseIdentifier:@"MyEmblemFourCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section == 3 ? 3:5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
            MyEmblemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyEmblemCell"];
//    cell.count = indexPath.section;
    if (indexPath.section == 0) {
            MyEmblemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyEmblemCell"];
        cell.pictuerImageView.image = [UIImage imageNamed:self.firstImagesArray[indexPath.row]];
        cell.minLabel.text = self.firstTitlesArray[indexPath.row];
        if (indexPath.row == 4) {
            cell.maxLabel.text = self.twoTitlesArray[0];
        }else{
            cell.maxLabel.text = self.firstTitlesArray[indexPath.row + 1];
        }
        return cell;
    }
    if (indexPath.section == 1) {
        MyEmblemTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyEmblemTwoCell"];
        cell.pictuerImageView.image = [UIImage imageNamed:self.twoImagesArray[indexPath.row]];
        cell.minLabel.text = self.twoTitlesArray[indexPath.row];
        if (indexPath.row == 4) {
            cell.maxLabel.text = self.threeTitlesArray[0];
        }else{
            cell.maxLabel.text = self.twoTitlesArray[indexPath.row + 1];
        }
        return cell;
    }
    if (indexPath.section == 2) {
        MyEmblemThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyEmblemThreeCell"];
        cell.pictuerImageView.image = [UIImage imageNamed:self.threetImagesArray[indexPath.row]];
        cell.minLabel.text = self.threeTitlesArray[indexPath.row];
        if (indexPath.row == 4) {
            cell.maxLabel.text = self.fourTitlesArray[0];
        }else{
            cell.maxLabel.text = self.threeTitlesArray[indexPath.row + 1];
        }
        return cell;
    }
    if (indexPath.section == 3) {
        MyEmblemFourCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyEmblemFourCell"];
        cell.pictuerImageView.image = [UIImage imageNamed:self.fourImagesArray[indexPath.row]];
        cell.minLabel.text = self.fourTitlesArray[indexPath.row];
        if (indexPath.row == 2) {
            cell.maxLabel.text = @"10K";
        }else{
            cell.maxLabel.text = self.fourTitlesArray[indexPath.row + 1];
        }
        return cell;
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return scaleTopAndBottom(140);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"huihzhangtou"];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view.mas_centerY);
        make.left.equalTo(view.mas_left).offset(scaleLeftAndRight(130));
        make.right.equalTo(view.mas_right).offset(scaleLeftAndRight(-130));
        make.height.mas_equalTo(scaleTopAndBottom(35));
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [imageView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.top.equalTo(imageView.mas_top);
    }];
    switch (section) {
        case 0:
            titleLabel.text = @"初级";
            break;
        case 1:
            titleLabel.text = @"中级";
            break;
        case 2:
            titleLabel.text = @"高级";
            break;
        case 3:
            titleLabel.text = @"特级";
            break;
        default:
            break;
    }
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return scaleTopAndBottom(50);
}
#pragma mark - UIScrollViewDelegate和取消区头的停滞效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]){
    }
    CGFloat sectionHeaderHeight = scaleTopAndBottom(50);
    if (scrollView.contentOffset.y <=sectionHeaderHeight && scrollView.contentOffset.y >=  0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

-(NSArray *)firstImagesArray{
    if (_firstImagesArray == nil) {
        _firstImagesArray = @[@"huizhangliang11",@"huizhangliang12",@"huizhangliang13",@"huizhangliang14",@"huizhangliang15"];
    }
    return _firstImagesArray;
}
-(NSArray *)twoImagesArray{
    if (_twoImagesArray == nil) {
        _twoImagesArray = @[@"huizhangliang21",@"huizhangliang22",@"huizhangliang23",@"huizhangliang24",@"huizhangliang25"];
    }
    return _twoImagesArray;
}
-(NSArray *)threetImagesArray{
    if (_threetImagesArray == nil) {
        _threetImagesArray = @[@"huizhangliang31",@"huizhangliang32",@"huizhangliang33",@"huizhangliang34",@"huizhangliang35"];
    }
    return _threetImagesArray;
}
-(NSArray *)fourImagesArray{
    if (_fourImagesArray == nil) {
        _fourImagesArray = @[@"huizhangliang41",@"huizhangliang42",@"huizhangliang43"];
    }
    return _fourImagesArray;
}
-(NSArray *)firstTitlesArray{
    if (_firstTitlesArray == nil) {
        _firstTitlesArray = @[@"0",@"100",@"200",@"400",@"700"];
    }
    return _firstTitlesArray;
}
-(NSArray *)twoTitlesArray{
    if (_twoTitlesArray == nil) {
        _twoTitlesArray = @[@"1100",@"1600",@"2100",@"2600",@"3100"];
    }
    return _twoTitlesArray;
}
-(NSArray *)threeTitlesArray{
    if (_threeTitlesArray == nil) {
        _threeTitlesArray = @[@"3700",@"4300",@"4900",@"5500",@"6100"];
    }
    return _threeTitlesArray;
}
-(NSArray *)fourTitlesArray{
    if (_fourTitlesArray == nil) {
        _fourTitlesArray = @[@"6700",@"7800",@"8800"];
    }
    return _fourTitlesArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
